using System.Collections.Generic;
using System.Drawing;

namespace DataStubGenerator.Controls
{
    public class SqlEditPresenter
    {
        #region Constructor

        private readonly ISqlEditView _sqlView;
        private readonly SqlModel _model;

        public SqlEditPresenter(ISqlEditView mainView,SqlModel model)
        {
            _sqlView = mainView;
            _sqlView.TextChanged += delegate { TextChanged(); };
            _model = model;
        }

        #endregion
        #region TextChanged

        public void TextChanged()
        {
            int originalPosition = _sqlView.SelectionStart;

            _model.Sql = _sqlView.Lines;

            List<Highlight> allHighlights = _model.GetAllKeywordHighlights();
            allHighlights.AddRange(_model.GetGreyWordHighlights());

            _sqlView.Lines = _model.Sql;

            foreach (Highlight highlight in allHighlights) 
                _sqlView.HighlightWord(highlight);

            _sqlView.SelectionStart = originalPosition;
            _sqlView.SelectionColor = SystemColors.WindowText;
        }

        #endregion
    }
}
